﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using GrainManage.Entities.Encrypt;
using GrainManage.Encrypt;
using System.Text.RegularExpressions;
using GrainManage.Server.Factory;

namespace GrainManage.Server.Business
{
    public class EncryptBusiness : BaseBusiness, GrainManage.Interface.IEncrypt
    {
        public OutputGetPublicKey GetPublicKey(InputGetPublicKey input)
        {

            var result = new OutputGetPublicKey();
            try
            {
                if (!string.IsNullOrWhiteSpace(input.UserName))
                {
                    var service = ObjectFactory.AccountService;
                    var account = service.GetAccountView(input.UserName);
                    if (account != null)
                    {
                        if (account.IsApproved)
                        {
                            var rsaFilePath = ObjectFactory.GetAppConfig("RsaKeyFilePath");
                            if (rsaFilePath.StartsWith("~") || !rsaFilePath.Contains(":"))
                            {
                                rsaFilePath = HttpContext.Current.Server.MapPath(rsaFilePath);
                            }
                            if (System.IO.File.Exists(rsaFilePath))
                            {
                                var xmlString = System.IO.File.ReadAllText(rsaFilePath);
                                result.PublicKey = RSAEncrypt.ExtractPublicKey(xmlString);
                                SetResponse(StatusCode.Success, result);
                            }
                            else
                            {
                                SetResponse(StatusCode.FileNotExist, result);
                            }
                        }
                        else
                        {
                            SetResponse(StatusCode.UserNotActivated, result);
                        }
                    }
                    else
                    {
                        SetResponse(StatusCode.NameNotExist, result);
                    }
                }
                else
                {
                    SetResponse(StatusCode.NameEmpty, result);
                }
            }
            catch (Exception e)
            {
                SetResponse(StatusCode.Failed, result, e.Message);
                WriteLog(System.Reflection.MethodBase.GetCurrentMethod(), e, input, result);
            }

            return result;
        }

        public OutputDesDecrypt DesDecrypt(InputDesDecrypt input)
        {
            var result = new OutputDesDecrypt();
            try
            {
                if (Regex.IsMatch(input.StringToDecrypt, @"^[A-Z0-9]*$"))
                {
                    result.DecryptedString = DESEncrypt.Decrypt(input.StringToDecrypt, input.Key);
                    SetResponse(StatusCode.Success, result);
                }
                else
                {
                    SetResponse(StatusCode.ToDecryptStringFormatNotMatch, result);
                }

            }
            catch (Exception e)
            {
                SetResponse(StatusCode.Failed, result, e.Message);
                WriteLog(System.Reflection.MethodBase.GetCurrentMethod(), e, input, result);
            }

            return result;
        }

        public OutputDesEncrypt DesEncrypt(InputDesEncrypt input)
        {
            var result = new OutputDesEncrypt();
            try
            {
                result.EncryptedString = DESEncrypt.Encrypt(input.StringToEncrypt, input.Key);
                SetResponse(StatusCode.Success, result);
            }
            catch (Exception e)
            {
                SetResponse(StatusCode.Failed, result, e.Message);
                WriteLog(System.Reflection.MethodBase.GetCurrentMethod(), e, input, result);
            }

            return result;
        }

        public OutputMd5Encrypt Md5Encrypt(InputMd5Encrypt input)
        {
            var result = new OutputMd5Encrypt();
            try
            {
                result.EncryptedString = MD5Encrypt.Encrypt(input.StringToEncrypt);
                SetResponse(StatusCode.Success, result);
            }
            catch (Exception e)
            {
                SetResponse(StatusCode.Failed, result, e.Message);
                WriteLog(System.Reflection.MethodBase.GetCurrentMethod(), e, input, result);
            }

            return result;
        }

        public OutputRsaDecrypt RsaDecrypt(InputRsaDecrypt input)
        {
            var result = new OutputRsaDecrypt();
            try
            {
                result.DecryptedString = RSAEncrypt.Decrypt(input.StringToDecrypt, input.PrivateKey);
                SetResponse(StatusCode.Success, result);
            }
            catch (Exception e)
            {
                SetResponse(StatusCode.Failed, result, e.Message);
                WriteLog(System.Reflection.MethodBase.GetCurrentMethod(), e, input, result);
            }

            return result;
        }

        public OutputRsaEncrypt RsaEncrypt(InputRsaEncrypt input)
        {
            var result = new OutputRsaEncrypt();
            try
            {
                result.EncryptedString = RSAEncrypt.Encrypt(input.StringToEncrypt, input.PublicKey);
                SetResponse(StatusCode.Success, result);
            }
            catch (Exception e)
            {
                SetResponse(StatusCode.Failed, result, e.Message);
                WriteLog(System.Reflection.MethodBase.GetCurrentMethod(), e, input, result);
            }

            return result;
        }


    }
}